﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CSSAdriverapp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

           // DriverMain openup = new DriverMain();//Bypass login
           // openup.Show();//Bypass login
           // this.Close();// Bypass login

        }

        private void logonButton_Click(object sender, RoutedEventArgs e) //login code here
        {
            try
            {

                DataClassesDataContext db = new DataClassesDataContext();

                IQueryable<User> users = from c in db.Users
                                         where c.UserName == usernameTextBox.Text &&
                                         c.PermissionLevelID == 1
                                         && c.Password == passwordBox.Password
                                         select c;
                users.ToList();

                if (users.Count() > 0) //If results from login info return, then go to main application
                {
                    DriverMain openup = new DriverMain();
                    openup.Show();
                    this.Close();
                }
                else
                {
                    errorLabel.Content = "Wrong Username or Password";
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }

            
        }

        private void passwordBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            logonButton_Click(sender, e);
        }
    }
}
